package com.ttt.mapper.impl;

import com.ttt.mapper.UserLoginInter;
import com.ttt.utils.DbConnectionUtil;

import java.sql.*;

public class UserLoginMapperImpl implements UserLoginInter {


    public boolean findByUser(String id) throws SQLException {

        //获取连接池
        DbConnectionUtil dbConnectionUtil = new DbConnectionUtil();
        //进行连接
        Connection connection = dbConnectionUtil.getConnection();
        String sql = "SELECT id FROM studentsregister WHERE id = ?";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            if (id.equals(resultSet.getString("id"))){
                return true;
            }
            resultSet.close();
            preparedStatement.close();
            connection.close();
        }
        return false;
    }


    public String findPassword(String id) throws SQLException {
        //获取连接池
        DbConnectionUtil dbConnectionUtil = new DbConnectionUtil();
        //进行连接
        Connection connection = dbConnectionUtil.getConnection();
        String sql =  "SELECT password FROM studentsregister WHERE id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        String password = null;
        while (resultSet.next()){
            password = resultSet.getString("password");
            System.out.println( "UserLoginMapperImpl password = " + password);
            return password;
        }
        return password;
    }


}
